Hello,
I am trying to use Boost's tcp::iostream, but it seems like there's something weird going on with peeking + reading.
Here is the minimum reproducing code -
server.cpp -
Code:
#include <iostream>
#include <boost/asio.hpp>
int main()
{
boost::asio::io_service io_service;
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 9858);
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
boost::asio::ip::tcp::iostream *stream = new boost::asio::ip::tcp::iostream;
boost::system::error_code ec;
acceptor.accept(*(stream->rdbuf()), ec); // checked ec, this succeeds
while (!stream->rdbuf()->available()) {}
std::cout << "bytes available: " << stream->rdbuf()->available() << std::endl;
char buf[5];
stream->read(buf, 5);
std::cout << "bytes available: " << stream->rdbuf()->available() << std::endl;
}
client.cpp
Code:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
int main()
{
boost::asio::ip::tcp::iostream *stream = new boost::asio::ip::tcp::iostream("localhost", boost::lexical_cast<std::string>(9858));
stream->write("asdegasdgasdhawehawehfaw", 10);
stream->flush();
}
Code:
g++ server.cpp -o server -lboost_system -pthread
g++ client.cpp -o client -lboost_system -pthread
if you want to compile them.
The server listens on port 9858 for a connection, accepts a connection, wait until number of bytes in read buffer goes > 0, prints number of bytes available, reads 5 bytes, and prints number of bytes available afterwards.
The client opens a TCP connection to the server, and sends 10 bytes.
The server output is:
Code:
bytes available: 10
bytes available: 0
I'm expecting
Code:
bytes available: 10
bytes available: 5
since only 5 bytes were read (the data read is correct).
Can someone take a stab at what may be going on?
Thanks!